package priv.utrix.micro.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import priv.utrix.micro.dao.AuthorityDao;
import priv.utrix.micro.entity.Authority;
import priv.utrix.micro.pojo.SysUserDO;
import priv.utrix.micro.service.AuthorityService;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限信息表(Authority)表服务实现类
 *
 * @author utrix
 * @since 2020-09-28 16:46:15
 */
@Service
@RequiredArgsConstructor
public class AuthorityServiceImpl extends ServiceImpl<AuthorityDao, Authority> implements AuthorityService {

    private final AuthorityDao authorityDao;

    @Override
    public SysUserDO listAuthorityByUsername(String username) {
        List<Authority> authorityList = authorityDao.listAuthorityByUsername(username);
        Preconditions.checkNotNull(authorityList);

        String[] authorityCodeList = authorityList.stream().map(Authority::getCode).toArray(String[]::new);
        String[] roleIdList = authorityList.stream().map(Authority::getRoleId).collect(Collectors.toList()).stream().distinct().toArray(String[]::new);

        return  new SysUserDO().setRoleIds(roleIdList).setAuthorityCodeList(authorityCodeList);
    }

    @Override
    public List<Authority> listAllRoleIdsGroupUrl() {
        List<Authority> authorities = authorityDao.selectAllRoleIdsGroupUrl();
        return authorities == null ? Collections.emptyList() : authorities;
    }
}
